// pages/travel/index.js
import {
    platform,
    tour,
    user,
    wxapp
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selAct: 0,
        value: '',
        weatherInfo: '',
        InSeasonList: [],
        districtList: [],
        district_id: '',
        active: 0,
        loadMoreText: '加载中...', //底部文字
        list: [],
        distanceMy: '',
        distance: '',
        selAct2: '',
        orderway: '',
        orderby: '',
        value1: 0,
        value2: 'a',
        latitude: '',
        longitude: '',
        categoryList: [],
        category_id: 2,
        show: false,
        focus: true,
        redeem_code: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getInfo()
    },
    toUrl() {
        wx.navigateTo({
            url: '/pages/couponRoute/index',
        })
    },
    toCheckCode() {
        if (this.data.redeem_code == '') {
            wx.showToast({
                title: '请输入兑换码',
                icon: 'error'
            })
            return false
        }
        tour.checkCode({
            redeem_code: this.data.redeem_code
        }, res => {
            if (res.code == 1) {
                if (res.data.status == 0) {
                    wx.showToast({
                        title: '无效兑换码',
                        icon: 'error'
                    })
                }
                if (res.data.status == 2) {
                    wx.showToast({
                        title: '该兑换码已使用',
                        icon: 'error'
                    })
                }
                if (res.data.status == 1) {
                    this.setData({
                        show: true
                    })
                    this.getList()
                    wx.setStorage({
                        key: "code",
                        data: this.data.redeem_code
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 获取列表
    getList(page) {
        let that = this
        tour.product.lists({}, res => {
            if (res.code == 1) {
                that.setData({
                    list: res.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let code = wx.getStorageSync('code')
        if (code) {
            tour.checkCode({
                redeem_code: code
            }, res => {
                if (res.code == 1) {
                    if (res.data.status == 2) {
                        this.setData({
                            show: false
                        })
                        wx.showToast({
                            title: '兑换码已使用',
                            icon: "none"
                        })
                    }
                    if (res.data.status == 1) {
                        this.setData({
                            show: true
                        })
                        this.getList()
                    }
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            })
        }
    },
    getInfo() {
        let token = wx.getStorageSync('token')
        if (token) {
            // 获取用户详情
            user.detailS({}, res => {
                if (res.code == 20003) {
                    this.getUserInfos()
                }
            })
        } else {
            this.getUserInfos()
        }
    },
    /**
     * 授权登录
     */
    getUserInfos(e) {
        wx.login({
            success(res) {
                console.log(res)
                let code = res.code
                wx.getUserInfo({
                    success: (res) => {
                        var encryptedData = res.encryptedData;
                        var iv = res.iv
                        var info = res.rawData
                        var obj = {
                            code: code,
                            iv: iv,
                            encryptedData: encryptedData
                        }
                        wxapp.login(obj, res => {
                            if (res.code == 1) {
                                wx.setStorage({
                                    key: "token",
                                    data: res.data.token
                                })
                            } else {
                                wx.showToast({
                                    title: res.msg,
                                    icon: 'none'
                                })
                            }
                        })
                    },
                    fail: res => {

                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})